package com.newhero.product.module.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.product.module.sys.model.Dict;
import com.newhero.product.module.sys.model.DictItem;
import com.newhero.product.module.sys.service.IDictItemService;
import com.newhero.product.module.sys.service.IDictService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: SysDictItemController
 * @Description: 访问控制类
 * @author Caolj
 * @date 2018-03-29
 */
@Controller
@RequestMapping("/admin")
public class DictItemController extends BaseController {
    
    @Autowired 
    private IDictItemService sysDictItemServiceImpl;
    @Autowired 
    private IDictService sysDictServiceImpl;
    																				
    /**
     * 列表页面
     * @return
     */    
	@RequestMapping(value = "/{dictId}/dictItemList", method ={RequestMethod.GET,RequestMethod.POST })
	public String dictItemList(
			@PathVariable String dictId,
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "itemName", defaultValue = "", required = true) String itemName,
			@RequestParam(value = "orderField", defaultValue = "ITEMORDER", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "1", required = true) int isAsc,//1-代表true;0-代表false
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<DictItem> p = new Page<DictItem>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<DictItem> entityWrapper = new EntityWrapper<DictItem>();
		entityWrapper.eq("DICTID", dictId);
		if(StringUtils.isNotBlank(itemName)){
			entityWrapper.like("ITEMNAME", itemName);
		}
		
		
	
		p = sysDictItemServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);

		Dict dict = sysDictServiceImpl.selectById(dictId);
		request.setAttribute("dict", dict);
		request.setAttribute("itemName", itemName);
		
		
		return "/sys/dict/dictItemList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/{dictId}/dictItemForm", method = RequestMethod.GET)
	public String dictItemForm(
			@PathVariable String dictId,
			String itemId, HttpServletRequest request) {
		
		Dict dict = sysDictServiceImpl.selectById(dictId);
		request.setAttribute("dict", dict);
		
		if (itemId != null && !"".equals(itemId)) {
			request.setAttribute("itemId", itemId);
			request.setAttribute("formData",sysDictItemServiceImpl.selectById(itemId));
		} else {
			request.setAttribute("itemId", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/sys/dict/dictItemForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/{dictId}/dictItemSave", method = RequestMethod.POST)
	public String dictItemSave(
			@PathVariable String dictId,
			String itemId, DictItem sysDictItem, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(itemId)) {
				model.addAttribute("flag", "数据修改成功！");
				sysDictItemServiceImpl.updateById(sysDictItem);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				sysDictItemServiceImpl.insert(sysDictItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:dictItemList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/{dictId}/dictItemDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String dictItemDelete(
			@PathVariable String dictId,
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		sysDictItemServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:dictItemList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/dictItemAjax", method = RequestMethod.POST)
	public Json sysDictItemAjax(String itemId) {
		DictItem sysDictItem = sysDictItemServiceImpl.selectById(itemId);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(sysDictItem);
		return json;
	}
    
}
